//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 

import delimited using "data\nyt_counties.csv", clear varn(1)
keep if state == "Georgia"
gen hoursfromclosing = clock(time,"YMD hms") - clock("2020-11-04 01:00:00","YMD hms")
drop if hours < 0
replace hours = hours/(60*60*1000)
sort name hoursfrom
gen finalvotes = votes if name ~= name[_n+1]
egen maxfinalvotes = max(finalvotes),by(name)
gen pctreported = 100*votes/maxfinalvotes
gen bidenpct = 100*bidenj/(bidenj + trumpd)
gen dbidenpct = 100*(bidenj - bidenj[_n-1])/(bidenj + trumpd - bidenj[_n-1] - trumpd[_n-1])
gen newvotes = bidenj + trumpd - bidenj[_n-1] - trumpd[_n-1] if name == name[_n-1]
drop if votes == votes[_n-1] & absentee_votes == absentee_votes[_n-1]
replace newvote = votes if name ~= name[_n-1]
sort name time votes
drop if votes == votes[_n-1] & absentee_votes == absentee_votes[_n-1]
gen inperson = votes - absentee_votes
replace dbidenpct = 100*bidenj/(bidenj + trumpd) if name ~= name[_n-1]



twoway (line pctreported hours,sort connect(stair) lcolor(sand)) ///
       (line bidenpct hours,sort connect(stair) lcolor(midblue)) ///
	   (scatter dbidenpct hours [aw=newvotes], sort msize(tiny) mcolor(midblue)) ///
	   if name == "Fulton" & hours <= 48 ///
	   , ///
	   xtitle(Hours from closing) ///
	   xlabel(0(4)48) ///
	   ytitle(" ") ///
	   ylabel(20 40 60 80 100 "100%") ///
	   legend(order(1 "Pct. of ballots reported" 2 "Biden vote share" 3 "Biden share of new votes") rows(2))
graph export "figures\fig15.png", replace width(2500)
//exit

** append a dummy observation at the 48 hour mark, to aid in graphing
preserve
keep if name ~= name[_n-1]
keep name 
gen pctreported = 101
save "data\scratch\gacounties", replace
restore
drop if pctreported > 100
append using "data\scratch\gacounties"
sort name pctreported
gen lastbiden = bidenj if pctreported[_n+1] == 101
gen lasttrump = trumpd if pctreported[_n+1] == 101
gen biden_win = lastbiden > lasttrump
egen max_biden_win = max(biden_win),by(name)

gen lastvotes = votes if pctreported[_n+1] == 101
egen max_lastvotes = max(lastvotes), by(name)

twoway (connected bidenpct pctreported if max_biden_win == 1 ,cmissing(n) lwidth(vthin) lpattern(solid) lcolor(midblue) msymbol(circle) msize(tiny)) ///
       (connected bidenpct pctreported if max_biden_win == 0 ,cmissing(n) lwidth(vthin) lpattern(solid) lcolor(red) msymbol(circle) msize(tiny)) ///
       (line bidenpct pctreported if name == "Fulton" ,cmissing(n) lwidth(thick) lpattern(solid) lcolor(midblue) msymbol(circle) msize(tiny)) ///
	   , ///
	   ytitle(Percent Biden two-party vote share) ///
	   xtitle(Percent reported) ///
	   legend(order(1 "Counties won by Biden" 2 "Counties won by Trump" 3 "Fulton County") rows(2))
graph export "figures\georgia_by_county.png", replace width(2500)

areg bidenpct pctreported if max_biden_win == 1 [aw=votes], a(name) robust
areg bidenpct pctreported if max_biden_win == 0 [aw=votes], a(name) robust


** what happens if we aggregate by "Biden" and "Trump" counties

preserve
use "data\scratch\gacounties",clear
capture drop pctreported
set obs 9540
gen hoursfromclosing = 1 in 1/159
replace hoursfromclosing = hoursfromclosing[_n-159] + 1 in 160/l
replace name = name[_n-159] in 160/l

save "data\scratch\gacounties60", replace
restore

sort name hoursfromclosing
replace hoursfromclosing = int(hoursfromclosing)
keep if hoursfromclosing ~= hoursfromclosing[_n+1]

keep name max_biden_win bidenj trumpd hoursfromclosing
merge 1:1 name hoursfromclosing using "data\scratch\gacounties60", nogen
replace trumpd = trumpd[_n-1] if name == name[_n-1] & trumpd == .
replace bidenj = bidenj[_n-1] if name == name[_n-1] & bidenj == .
replace max_biden_win = max_biden_win[_n-1] if name == name[_n-1] & max_biden_win == .
collapse (sum) trumpd bidenj,by(hoursfromclosing max_biden_win)
drop if max_biden_win == .
reshape wide trumpd bidenj,i(hoursfromclosing) j(max_biden_win)
gen bidenpct0 = 100*bidenj0/(bidenj0 + trumpd0)
gen bidenpct1 = 100*bidenj1/(bidenj1 + trumpd1)